<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


switch( @$URL[1] )
{
    case '':
    default:    
        $_page_content .= $current_page->fetch( 'worker.tpl' );
        
        break;
    case 'modify':
        
        $worker_id = @$URL[2];
        $worker_data = $Worker->find_by( 'id', $worker_id );
        $current_page->assign( 'worker', $worker_data );
        
        $current_page->assign( 'modify', true );
        
    case 'add':
        
        $hair_colors_bulk = $Hair_color_cache->get_all();
        $eyes_colors_bulk = $Eyes_color_cache->get_all();
        
        $hair_colors = array();
        $eyes_colors = array();
        
        if ( is_array( $hair_colors_bulk ) )
        {
            foreach( $hair_colors_bulk as $hc )
            {
                $hair_colors[] = $hc[$Hair_color_cache->TABLE_COLUMNS['hair_color']];
            }
        }
        
        if ( is_array( $eyes_colors_bulk ) )
        {
            foreach( $eyes_colors_bulk as $ec )
            {
                $eyes_colors[] = $ec[$Eyes_color_cache->TABLE_COLUMNS['eyes_color']];
            }
        }
        
        $current_page->assign( 'eyes_colors', $eyes_colors );
        $current_page->assign( 'hair_colors', $hair_colors );
        
        $countries = $Country->get_all();
        $current_page->assign( 'countries', $countries );        
        
        $_page_content .= $current_page->fetch( 'worker_add_modify.tpl' );
        
        break;
    case 'add-action':
        
        $data = array( 
            'city'                  => $_POST['city'],
            'first_name'            => $_POST['first_name'],
            'last_name'             => $_POST['last_name'],
            'email'                 => $_POST['email'],
            'phone'                 => $_POST['phone'],
            'birth_date'            => ( $_POST['birth_date_year'] .'-'. $_POST['birth_date_month'] .'-'. $_POST['birth_date_day'] ),
            'occupation'            => $_POST['occupation'],
            'sex'                   => $_POST['sex'],
            'weight'                => $_POST['weight'],
            'height'                => $_POST['height'],
            'dimension_1'           => $_POST['dimension_1'],
            'dimension_2'           => $_POST['dimension_2'],
            'dimension_3'           => $_POST['dimension_3'],
            'shoe'                  => $_POST['shoe_size'],
            'hair'                  => $_POST['hair_color'],
            'dyed_hair'             => $_POST['dyed_hair_color'],
            'eyes'                  => $_POST['eyes_color'],
            'sampling'              => ( @$_POST['activity_sampling'] == 'on' ? 1 : 0 ),
            'merchandising'         => ( @$_POST['activity_merchandising'] == 'on' ? 1 : 0 ),
            'promotion'             => ( @$_POST['activity_promotion'] == 'on' ? 1 : 0 ),
            'caravan'               => ( @$_POST['activity_caravan'] == 'on' ? 1 : 0 ),
            'mistery_shopper'       => ( @$_POST['activity_mistery_shopper'] == 'on' ? 1 : 0 ),
            'door_to_door'          => ( @$_POST['activity_door_to_door'] == 'on' ? 1 : 0 ),
            'english'               => ( @$_POST['language_english'] == 'on' ? 1 : 0 ),
            'romanian'              => ( @$_POST['language_romanian'] == 'on' ? 1 : 0 ),
            'french'                => ( @$_POST['language_french'] == 'on' ? 1 : 0 ),
            'german'                => ( @$_POST['language_german'] == 'on' ? 1 : 0 ),
            'language_other'        => $_POST['language_other'],
            'details'               => nl2br( filter_cross( trim( $_POST['details'] ) ) ),
            );
        
        $hair_color_found = $Hair_color_cache->find_by( 'hair_color', $data['hair'] );
        if ( count( $hair_color_found ) == 0 )
        {
            $Hair_color_cache->add( array( 'hair_color' => $data['hair'] ) );
        }
        
        $eyes_color_found = $Eyes_color_cache->find_by( 'eyes_color', $data['eyes'] );
        if ( count( $hair_color_found ) == 0 )
        {
            $Eyes_color_cache->add( array( 'eyes_color' => $data['eyes'] ) );
        }
        
        $added = $Worker->add( $data );

        foreach( $_POST['pictures'] as $pic )
        {
            $pic_name = str_replace( 'temp_pictures/workers/', '', $pic );
            $pic_name = str_replace( '.jpg', '', $pic_name );

            $new_name = $_base_path .'/pictures_added/workers/'. $pic_name .'.jpg';            
            rename( $pic, $new_name );
            
            $Files_worker->add( 
                    array( 
                        'name'      =>  $new_name,
                        'worker'    =>  $added
                        ) 
                    );
        }        
        
        break;
        
    case 'upload_image':
        
        if (!empty($_FILES)) 
        {
            $tempFile = $_FILES['Filedata']['tmp_name'];
            $targetPath = $_base_path .'/'. 'temp_pictures' . '/workers/';
            $targetFile =  str_replace('//','/',$targetPath) . rand_str( 32 ) .'.jpg';

            move_uploaded_file($tempFile,$targetFile);
            echo str_replace($_base_path .'/','',$targetFile);
            
            $_output = false;                          
        }
        
        break;
        
    case 'delete_image':
        
        $image_to_del = $_base_path .'/'. $_POST['image'];
        unlink( $image_to_del );
        
        break;
    
    case 'search':
        
        $_page_content .= $current_page->fetch( 'worker_search.tpl' );
        
        break;
    
    case 'search-action':
        
        $likes = json_decode( $_POST['likes'] );
        $equal = json_decode( $_POST['equal'] );
        $interval = json_decode( $_POST['interval'] );
        
        $workers_found = $Worker->search( $likes, $interval, $equal );
        
        $_ajax_data['workers'] = $workers_found;
        
        break;
        
    /*
    case 'list':
        
        $worker_types = $Worker_type->get_all();
        $current_page->assign( 'worker_types', $worker_types );
        
        $workers = $Worker->get_all();
        $current_page->assign( 'workers', $workers );
        $_page_content .= $current_page->fetch( 'worker_list.tpl' );
        break;
     * 
     */
}


?>
